package com.kollway.update.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.widget.RemoteViews;

import com.kollway.update.R;

/**
 * User: Yaotian Leung
 * Date: 2017-01-22
 * Time: 18:46
 */
public final class NotificationUtil {
    public static final String TAG = "NotificationUtil";
    private final NotificationManager notificationManager;
    private final Context appContext;

    public NotificationUtil(Context context){
        appContext = context.getApplicationContext();
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public Notification.Builder builder(int icon, String title, String message, boolean autoCancel, boolean ongoing, Uri sound, PendingIntent pendingIntent){
        Notification.Builder builder = new Notification.Builder(appContext);
        builder.setContentIntent(pendingIntent);
        builder.setTicker(!TextUtils.isEmpty(title) ? title : appContext.getResources().getString(R.string.update));
        builder.setContentTitle(!TextUtils.isEmpty(title) ? title : appContext.getResources().getString(R.string.update));
        builder.setContentText(message);
        builder.setAutoCancel(autoCancel);
        builder.setOngoing(ongoing);
        builder.setSmallIcon(icon == 0 ? R.drawable.ic_downloading : icon);
        if(sound != null){
            builder.setSound(sound);
        }
        return builder;
    }

    public Notification.Builder builder(int icon, RemoteViews views, boolean autoCancel, boolean ongoing, Uri sound, PendingIntent pendingIntent){
        Notification.Builder builder = new Notification.Builder(appContext);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(autoCancel);
        builder.setOngoing(ongoing);
        builder.setSmallIcon(icon == 0 ? R.drawable.ic_downloading : icon);
        if(sound != null){
            builder.setSound(sound);
        }
        builder.setContent(views);
        return builder;
    }

    public void show(int id, Notification.Builder builder) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
            notificationManager.notify(id, builder.build());
        }else{
            notificationManager.notify(id, builder.getNotification());
        }
    }

    public void hide(int id){
        notificationManager.cancel(id);
    }

}

